﻿// ActionScript file
/**
 * description:以某一点为中心、自适应child宽高的容�?child填满本容�?
 * author	:com.nickro
 * 
 * USAGE	:
 * 			var ffBox:FullFilledBox=new FullFilledBox();
 * 			addChild(ffBox);
 *		 	ffBox.x/y=(Number);
 *		 	ffBox.centerPoint=new Point((Number),(Number));
 * 			ffBox.addContent((DisplyObject));
 * 
 * 
 * expands	:
 * 			1.添加(改变容器尺寸onResize)效果
 * 			2.添加(移除前一子项)效果
 */
package org.nickro.controls{
	import caurina.transitions.*;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	
	public class FullFilledBox extends BaseControls{
		private var curWidth:Number=0;
		private var curHeight:Number=0;
		private var tarWidth:Number;
		private var tarHeight:Number;
		
		private var centerX:Number=0;
		private var centerY:Number=0;
		
		private var _child:DisplayObject;
		private var speed:Number;
		private var _scaleX:Number;
		private var _scaleY:Number;
		private var cenPoint:Point=new Point();
		private var isInit:Boolean=true;
		public function FullFilledBox(){
			super();
		}
		/**
		 *难点:容器宽高改变会改变容器的缩放�?->导致后续添加内容都失去原有尺寸，应该除以此缩放比，保持添加内容不缩放-->
		 * 添加新内容时，还原前次图像的尺寸
		 * 
		 *流程:添加内容(对旧图像效果处理)-->改变容器宽高-->移除旧内�?&还原旧图像尺�?->添加新内�?->改变新内容尺�?
		 */
		public function addContent(child:DisplayObject):void{
			/* if(this.numChildren==1){
				var removeOutObject:Object={};
				removeOutObject.alpha=0;
				removeOutObject.time=.3;
				Tweener.addTween(DisplayObject(getChildAt(0)),removeOutObject);
			}  */
			
			_child=child;
			tarWidth=_child.width;
			tarHeight=_child.height;
			if(isInit){
				centerX=this.x+cenPoint.x;
				centerY=this.y+cenPoint.y;
			
				_scaleX=cenPoint.x/tarWidth;
				_scaleY=cenPoint.y/tarHeight;
				
			}
			onResize();
		}
		
		//改变容器的大小、宽�?
		private function onResize():void{
			if(isInit){
				isInit=false;
				addChild(_child);
				return;
			}else{
				var dx:Number= (centerX-(tarWidth*_scaleX));
				var dy:Number= (centerY-(tarHeight*_scaleY));
				var tweenObj:Object={};
				tweenObj.time=.5;
				tweenObj.x=dx;
				tweenObj.y=dy;
				tweenObj.width=tarWidth;
				tweenObj.height=tarHeight;
				tweenObj.onComplete=sendRender;
				Tweener.addTween(this,tweenObj);
			}
		}

		private function sendRender():void{
			//还原前次图像大小
			getChildAt(0).width=curWidth;
			getChildAt(0).height=curHeight;
			removeChildAt(0);
			setChanged();
		}
		
		//更新屏幕，并给centerX,centerY,curWidth,curHeight赋�?
		override protected function updateDislayObject():void{
			addChild(_child);
			//因为此容器的scaleX/scaleY发生变化，所以要根据此来调整图片的大�?
			_child.width=tarWidth/this.scaleX;
			_child.height=tarHeight/this.scaleY;
			
			curWidth=tarWidth;
			curHeight=tarHeight;
		}
		public function set centerPoint(value:Point):void{
			cenPoint=value;
		}
	
		public function set bg(value:*):void{
			
		}
		
	}
}